package com.suyeer.basic.util;

import com.suyeer.basic.enums.RegExTypeEnum;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author jun 2019/1/15.
 */
public class RegExUtil {

    /**
     * 获取最短匹配字符串数组
     *
     * @param srcStr   String
     * @param startStr String
     * @param endStr   String
     * @return List
     */
    public static List<String> getLeastMatcherList(String srcStr, String startStr, String endStr) {
        return getLeastMatcherList(srcStr, startStr, endStr, RegExTypeEnum.WITH_START_WITH_END);
    }

    /**
     * 获取最短匹配字符串数组
     *
     * @param srcStr    String
     * @param startStr  String
     * @param endStr    String
     * @param regExType RegExTypeEnum
     * @return List
     */
    public static List<String> getLeastMatcherList(String srcStr, String startStr, String endStr, RegExTypeEnum regExType) {
        List<String> retList = new ArrayList<>();
        String[] arr = srcStr.split(changeSpecialWord(startStr));
        String regex = String.format(regExType.getRegex(), changeSpecialWord(startStr), changeSpecialWord(endStr));
        for (int i = 0; i < arr.length; i++) {
            String tmpSrc = startStr + arr[i] + startStr;
            if (i == 0) {
                tmpSrc = arr[i] + startStr;
            }
            if (i == (arr.length - 1)) {
                tmpSrc = startStr + arr[i];
            }
            retList.addAll(getMatcherList(tmpSrc, regex));
        }
        return retList;
    }

    /**
     * 获取正则匹配的 string 数组
     *
     * @param srcStr   String
     * @param startStr String
     * @param endStr   String
     * @return List
     */
    public static List<String> getMatcherList(String srcStr, String startStr, String endStr) {
        return getMatcherList(srcStr, startStr, endStr, RegExTypeEnum.WITH_START_WITH_END);
    }

    /**
     * 获取正则匹配的 string 数组
     *
     * @param srcStr    String
     * @param startStr  String
     * @param endStr    String
     * @param regExType RegExTypeEnum
     * @return List
     */
    public static List<String> getMatcherList(String srcStr, String startStr, String endStr, RegExTypeEnum regExType) {
        String regex = String.format(regExType.getRegex(), changeSpecialWord(startStr), changeSpecialWord(endStr));
        return getMatcherList(srcStr, regex);
    }

    /**
     * 获取正则匹配的 string 数组
     * 特殊字符 '$', '(', ')', '*', '+', '.', '[', ']', '?', '\\', '^', '{', '}', '|'
     *
     * @param srcStr string
     * @param regex  string 若包含特殊字符, 需要添加双斜杠转译
     * @return List
     */
    public static List<String> getMatcherList(String srcStr, String regex) {
        List<String> retList = new ArrayList<>();
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(srcStr);
        while (matcher.find()) {
            retList.add(matcher.group());
        }
        return retList;
    }

    private static String changeSpecialWord(String str) {
        try {
            return str.replace("\\", "\\\\").replace("$", "\\$")
                    .replace("(", "\\(").replace(")", "\\)")
                    .replace("*", "\\*").replace("+", "\\+")
                    .replace(".", "\\.").replace("?", "\\?")
                    .replace("^", "\\^").replace("|", "\\|")
                    .replace("{", "\\{").replace("}", "\\}")
                    .replace("[", "\\[").replace("]", "\\]");
        } catch (Exception e) {
            LogUtil.error(e);
        }
        return StringUtils.EMPTY;
    }

}
